package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.DailyTrainSeat;
import com.jiawa.train.business.domain.DailyTrainSeatExample;
import com.jiawa.train.business.domain.TrainSeat;
import com.jiawa.train.business.domain.TrainStation;
import com.jiawa.train.business.mapper.DailyTrainSeatMapper;
import com.jiawa.train.business.req.DailyTrainSeatQueryReq;
import com.jiawa.train.business.req.DailyTrainSeatSaveReq;
import com.jiawa.train.common.resp.PageResult;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DailyTrainSeatService {
    //reource是使用名称注入的方式而autowired是使用类型注入的方式
    @Resource
    private DailyTrainSeatMapper dailyTrainSeatMapper;
    @Resource
    private TrainSeatService trainSeatService;
    @Resource
    private TrainStationService trainStationService;

    //新增或者更新用户信息，通过判断是否有id决定进行新增操作还是更新操作
    public void save(DailyTrainSeatSaveReq req) {
        //将传递的参数赋给DailyTrainSeat对象,在req中没有id,createTime,updateTime的数据，需要自己进行赋值
        //这里的id使用雪花算法，然后创建时间使用当前时间
        DateTime now = DateTime.now();
        DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(req, DailyTrainSeat.class);
        //如果id为空，则进行新增操作
        if(ObjectUtil.isNull(dailyTrainSeat.getId())){
            dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainSeat.setCreateTime(now);
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        }
        //否则进行更新操作
        else{
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeatMapper.updateByPrimaryKeySelective( dailyTrainSeat);
        }
    }

    //如果用户id存在则进行根据id进行查询乘客的信息，如果是用户id不存在则进行查询所有,全部都按分页进行处理
    public PageResult queryDailyTrainSeatList(DailyTrainSeatQueryReq  req) {
        //创建dailyTrainSeatExample对象
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        dailyTrainSeatExample.setOrderByClause("date desc, train_code asc, carriage_index asc, carriage_seat_index asc");
        //设置查询条件
        DailyTrainSeatExample.Criteria criteria = dailyTrainSeatExample.createCriteria();

        if(ObjectUtil.isNotNull(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        // 先设置分页的参数
        PageHelper.startPage(req.getPage(),req.getPageSize());
        //进行查询数据库
        List<DailyTrainSeat> dailyTrainSeatList = dailyTrainSeatMapper.selectByExample(dailyTrainSeatExample);
        //返回查询结果,转为分页的形式,
        PageInfo<DailyTrainSeat> pageInfo = new PageInfo<>(dailyTrainSeatList);
        //获取数据进行封装
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        //返回查询结果
        return pageResult;
    }

    public void delete(Long id) {
        dailyTrainSeatMapper.deleteByPrimaryKey(id);
    }

    //根据日期生成每日座位信息
    @Transactional
    public void genDailySeat(Date date,String trainCode) {
        //1.删除日期date的某车次的信息
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        DailyTrainSeatExample.Criteria criteria = dailyTrainSeatExample.createCriteria();
        criteria.andDateEqualTo(date);
        criteria.andTrainCodeEqualTo(trainCode);
        dailyTrainSeatMapper.deleteByExample(dailyTrainSeatExample);
        //2.查询所有的车座信息
       List<TrainSeat> trainSeatList = trainSeatService.selectByTrainCode(trainCode);
       if(CollUtil.isEmpty(trainSeatList)){
           log.info("没有查询到车座信息");
           return;
       }
       //3.查询该车次火车车站的信息
        List<TrainStation> trainStationList = trainStationService.selectByTrainCode(trainCode);
        String sell = StrUtil.fillBefore("", '0', trainStationList.size() - 1);
       //4.生成每日座位信息
       for(TrainSeat trainSeat : trainSeatList){
           DateTime now = new DateTime();
           DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(trainSeat, DailyTrainSeat.class);
           dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
           dailyTrainSeat.setDate(date);
           dailyTrainSeat.setCreateTime(now);
           dailyTrainSeat.setUpdateTime(now);
           dailyTrainSeat.setSell(sell);
           dailyTrainSeatMapper.insert(dailyTrainSeat);
       }
    }

    //根据日期，车次和车座的类型来进行查询一等座/二等座/软座/硬座的个数
    public int countSeat(Date date, String trainCode, String seatType) {
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        DailyTrainSeatExample.Criteria criteria = dailyTrainSeatExample.createCriteria();
        criteria.andDateEqualTo(date);
        criteria.andTrainCodeEqualTo(trainCode);
        criteria.andSeatTypeEqualTo(seatType);
        long l =  dailyTrainSeatMapper.countByExample(dailyTrainSeatExample);
        //如果获取到的软座或者硬座的个数为0,则返回-1；
        if(l == 0L){
            return -1;
        }else{
            return (int) l;
        }
    }

    public List<DailyTrainSeat> selectSeatsByCarriageIndexAndSeatType(Date date, String trainCode, Integer index, String seatTypeCode) {
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        dailyTrainSeatExample.setOrderByClause("carriage_seat_index asc");
        DailyTrainSeatExample.Criteria criteria = dailyTrainSeatExample.createCriteria();
        criteria.andDateEqualTo(date);
        criteria.andTrainCodeEqualTo(trainCode);
        criteria.andCarriageIndexEqualTo(index);
        criteria.andSeatTypeEqualTo(seatTypeCode);
        return dailyTrainSeatMapper.selectByExample(dailyTrainSeatExample);
    }
}